from sqlalchemy import Column, Integer, String, Boolean, ForeignKey
from auth.db import Base
from datetime import datetime, timedelta, timezone

# 东八区时区对象（中国标准时间）
CST = timezone(timedelta(hours=8))

def now_cst():
    """返回当前北京时间（带时区信息）"""
    return datetime.now(CST)

class Device(Base):
    __tablename__ = "devices"
    # Ensure table uses utf8mb4 for Chinese support
    __table_args__ = {'mysql_charset': 'utf8mb4', 'mysql_collate': 'utf8mb4_unicode_ci'}

    id = Column(Integer, primary_key=True, index=True)
    device_id = Column(String(64), unique=True, index=True, nullable=False)
    name = Column(String(100), nullable=True)
    owner = Column(String(50), ForeignKey("users.username", ondelete="CASCADE", onupdate="CASCADE"), nullable=True)
    online = Column(Boolean, default=False)
    created_at = Column(String(50), default=now_cst().strftime("%Y-%m-%d %H:%M:%S"))